Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Reflection
Imports System.IO
Imports Microsoft.Win32

<System.ComponentModel.RunInstaller(True)> _
Public Class InstallerClass
    Inherits System.Configuration.Install.Installer
    Private Const SmartPhoneINI_FILE As String = "SmartphoneClient.ini"
    Private Const DotNetINI_FILE As String = "NETCFv2.ini"

    Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        MyBase.Install(stateSaver)
        If (Context.Parameters("installvst") <> String.Empty) Then
            InstallVsTemplate(True)
            stateSaver.Add("installvst", "True")
        End If
        If (Context.Parameters("installsc") <> String.Empty) Then
            ExecuteSmartphoneClientInstaller(True)
            stateSaver.Add("installsc", "True")
        End If
        If (Context.Parameters("installncf") <> String.Empty) Then
            ExecuteDotNetCFInstaller(True)
            stateSaver.Add("installncf", "True")
        End If

    End Sub

    Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
        MyBase.Uninstall(savedState)
        If (savedState("installsc") <> String.Empty Or savedState("installncf") <> String.Empty) Then
            ExecuteCeApp(True)
        End If
        If savedState("installvst") <> String.Empty Then
            RemoveVsTemplate(True)
        End If
    End Sub


    Private Sub ExecuteSmartphoneClientInstaller(ByVal wait As Boolean)
        Dim ceAppPath As String = GetWindowsCeApplicationManager()
        If ceAppPath = String.Empty Then
            Return
        End If
        Dim iniPath As String = GetIniPath(SmartPhoneINI_FILE)
        Dim SmartphoneClientProcess As New System.Diagnostics.Process()
        SmartphoneClientProcess.StartInfo.FileName = ceAppPath
        SmartphoneClientProcess.StartInfo.Arguments = iniPath
        SmartphoneClientProcess.Start()
        If (wait = True) Then
            SmartphoneClientProcess.WaitForExit()
        End If
        SmartphoneClientProcess.Close()
    End Sub
    Private Sub ExecuteDotNetCFInstaller(ByVal wait As Boolean)
        Dim ceAppPath As String = GetWindowsCeApplicationManager()
        If ceAppPath = String.Empty Then
            Return
        End If
        Dim iniPath As String = GetIniPath(DotNetINI_FILE)
        Dim DotNetProcess As New System.Diagnostics.Process()
        DotNetProcess.StartInfo.FileName = ceAppPath
        DotNetProcess.StartInfo.Arguments = iniPath
        DotNetProcess.Start()
        If (wait = True) Then
            DotNetProcess.WaitForExit()
        End If
        DotNetProcess.Close()
    End Sub
    Private Sub ExecuteCeApp(ByVal wait As Boolean)
        Dim ceAppPath As String = GetWindowsCeApplicationManager()
        If ceAppPath = String.Empty Then
            Return
        End If
        Dim iniPath As String = GetIniPath(DotNetINI_FILE)
        Dim DotNetProcess As New System.Diagnostics.Process()
        DotNetProcess.StartInfo.FileName = ceAppPath
        DotNetProcess.Start()
        If (wait = True) Then
            DotNetProcess.WaitForExit()
        End If
        DotNetProcess.Close()
    End Sub

    Private Sub InstallVsTemplate(ByVal wait As Boolean)
        Dim FolderPath As String = GetVsTemplateFolderPath()
        If FolderPath = String.Empty Then
            Return
        End If
        Dim DevEnvPath As String = GetVsDevEnvPath()
        If DevEnvPath = String.Empty Then
            Return
        End If
        Dim Indexfile As String = Path.Combine(Path.GetDirectoryName( _
           System.Reflection.Assembly.GetExecutingAssembly().Location), "Templateindex.vstdir")
        Dim Templatefile As String = Path.Combine(Path.GetDirectoryName( _
           System.Reflection.Assembly.GetExecutingAssembly().Location), "SMS Service.zip")
        System.IO.Directory.CreateDirectory(FolderPath + "\Csharp\SMS Server\1033")
        System.IO.File.Copy(Indexfile, FolderPath + "\Csharp\SMS Server\Templateindex.vstdir", True)
        System.IO.File.Copy(Templatefile, FolderPath + "\Csharp\SMS Server\1033\SMS Service.zip", True)
        Dim DotNetProcess As New System.Diagnostics.Process()
        DotNetProcess.StartInfo.FileName = DevEnvPath
        DotNetProcess.StartInfo.Arguments = "/installvstemplates"
        DotNetProcess.Start()
        If (wait = True) Then
            DotNetProcess.WaitForExit()
        End If
        DotNetProcess.Close()
    End Sub

    Private Sub RemoveVsTemplate(ByVal wait As Boolean)
        Dim FolderPath As String = GetVsTemplateFolderPath()
        If FolderPath = String.Empty Then
            Return
        End If
        Dim DevEnvPath As String = GetVsDevEnvPath()
        If DevEnvPath = String.Empty Then
            Return
        End If
        If System.IO.Directory.Exists(FolderPath + "\Csharp\SMS Server") = True Then
            System.IO.Directory.Delete(FolderPath + "\Csharp\SMS Server", True)
            Dim DotNetProcess As New System.Diagnostics.Process()
            DotNetProcess.StartInfo.FileName = DevEnvPath
            DotNetProcess.StartInfo.Arguments = "/installvstemplates"
            DotNetProcess.Start()
            If (wait = True) Then
                DotNetProcess.WaitForExit()
            End If
            DotNetProcess.Close()
        End If
    End Sub


    Public Shared Function GetWindowsCeApplicationManager() As String
        '---check if the Windows CE Application Manager is installed---
        Dim ceAppPath As String = AppMgrKeyExists()
        If ceAppPath = String.Empty Then
            MessageBox.Show("Windows CE App Manager not installed", _
                            "Setup", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
            Return String.Empty
        Else
            Return ceAppPath
        End If
    End Function

    Public Shared Function GetIniPath(ByVal IniName As String) As String
        '---get the path of the .ini file---
        Return """" & _
           Path.Combine(Path.GetDirectoryName( _
           System.Reflection.Assembly. _
           GetExecutingAssembly().Location), IniName) & """"
    End Function

    Private Shared Function AppMgrKeyExists() As String
        '---get the path to the Windows CE App Manager from the registry---
        Dim key As RegistryKey = _
           Registry.LocalMachine.OpenSubKey( _
           "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\CEAPPMGR.EXE")
        If key Is Nothing Then
            Return String.Empty
        Else
            Return key.GetValue(String.Empty, String.Empty)
        End If
    End Function

    Public Shared Function GetVsTemplateFolderPath() As String
        '---check if the Windows CE Application Manager is installed---
        Dim VsTempPath As String = VsTemplateKeyExists()
        If VsTempPath = String.Empty Then
            MessageBox.Show("VS templates folder not found", _
                            "Setup", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
            Return String.Empty
        Else
            Return VsTempPath
        End If
    End Function
    Public Shared Function GetVsDevEnvPath() As String
        '---check if the Windows CE Application Manager is installed---
        Dim VsTempPath As String = VsDevEnvExists()
        If VsTempPath = String.Empty Then
            MessageBox.Show("DEVENV.exe not found", _
                            "Setup", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
            Return String.Empty
        Else
            Return VsTempPath
        End If
    End Function


    Private Shared Function VsDevEnvExists() As String
        '---get the path to the Windows CE App Manager from the registry---
        Dim key As RegistryKey = _
           Registry.LocalMachine.OpenSubKey( _
           "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\DEVENV.EXE")
        'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\VSTemplate\Project
        If key Is Nothing Then
            Return String.Empty
        Else
            Return key.GetValue(String.Empty, String.Empty)
        End If
    End Function
    Private Shared Function VsTemplateKeyExists() As String
        '---get the path to the Windows CE App Manager from the registry---
        Dim key As RegistryKey = _
           Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\VisualStudio\8.0\VSTemplate\Project")
        '   "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\DEVENV.EXE")
        'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\VSTemplate\Project
        If key Is Nothing Then
            Return String.Empty
        Else
            Return key.GetValue("UserFolder", String.Empty)
        End If
    End Function

End Class
